package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 自定义菜单模型
 * 
 * @author ultrafrog
 * @version 1.0, 2014-12-16
 * @since 1.0
 */
@Entity
@Table(name = "uwx_menu")
public class Menu extends Model {

	/** 名称 */
	@Column(name = "vc_name")
	public String name;
	
	/** 类型 */
	@Column(name= "vc_type")
	public String type;
	
	/** 键值 */
	@Column(name = "vc_key")
	public String key;
	
	/** 地址 */
	@Column(name = "vc_url")
	public String url;
	
	/** 顺序 */
	@Column(name = "nb_order")
	public Integer order;
	
	/** 上级编号 */
	@Column(name = "fk_parent")
	public String parent;
	
	/** 公众号 */
	@Column(name = "fk_weixin")
	public String weixin;
	
	/** 下级 */
	@Transient
	private List<Menu> children;
	
	/** 构造函数 */
	public Menu() {}
	
	/**
	 * 构造函数 
	 * 
	 * @param name
	 * @param type
	 * @param key
	 * @param url
	 * @param order
	 * @param parent
	 * @param weixin
	 */
	public Menu(String name, String type, String key, String url, Integer order, String parent, String weixin) {
		this();
		this.name = name;
		this.type = type;
		this.key = key;
		this.url = url;
		this.order = order;
		this.parent = parent;
		this.weixin = weixin;
	}
	
	/**
	 * 读取下级
	 * 
	 * @return
	 */
	public List<Menu> getChildren() {
		if (children == null) {
			children = Menu.find("parent = ? order by order", id).fetch();
		}
		return children;
	}
}
